package com.ktgj.service.service.impl;

import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.domain.R;
import com.ktgj.common.core.enums.EmHelper;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.common.core.utils.StringUtils;
import com.ktgj.common.core.utils.helper.StringHelper;
import com.ktgj.flight.api.RemoteFlightService;
import com.ktgj.flight.api.domain.AirportApi;
import com.ktgj.flight.api.domain.AirportTerminalApi;
import com.ktgj.flight.api.domain.Terminal;
import com.ktgj.hotel.api.RemoteHotelService;
import com.ktgj.hotel.api.domain.DicCityApi;
import com.ktgj.hotel.api.domain.DicCountyVoApi;
import com.ktgj.service.domain.*;
import com.ktgj.service.domain.vo.TmpViphallVo;
import com.ktgj.service.domain.vo.VipHallVipChannelPrice;
import com.ktgj.service.domain.vo.ViphallPatamter;
import com.ktgj.service.mapper.*;
import com.ktgj.service.service.IKtserviceViphallTagService;
import com.ktgj.service.service.IVipchannelService;
import com.ktgj.service.service.IViphallCommentService;
import com.ktgj.service.service.IViphallService;
import com.ktgj.train.api.RemoteTrainService;
import com.ktgj.train.api.domain.CityVoApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

//import com.ktgj.customer.api.RemoteCustomerService;


/**
 * 贵宾厅Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-03
 */
@Service
@Transactional
public class ViphallServiceImpl implements IViphallService {
    @Autowired
    private ViphallMapper viphallMapper;

    //贵宾厅中间表
    @Autowired
    private ContractLinkViphallMapper contractLinkViphallMapper;

    //贵宾厅价格维护
    @Autowired
    private ContractLinkViphallPriceMapper contractLinkViphallPriceMapper;

    @Autowired
    private RemoteFlightService remoteFlightService;

    @Autowired
    private IVipchannelService vipchannelService;

    @Autowired
    private IViphallCommentService viphallCommentService;

    @Autowired
    private ViphallImagedescMapper viphallImagedescMapper;

    @Autowired
    private ViphallAttachmentMapper viphallAttachmentMapper;

    @Autowired
    private IKtserviceViphallTagService ktserviceViphallTagService;

    @Autowired
    private ViphallLinkTagMapper viphallLinkTagMapper;

    @Autowired
    private ViphallServiceInstructionsMapper viphallServiceInstructionsMapper;

    @Autowired
    private ViphalllLinkAuxtypeMapper viphalllLinkAuxtypeMapper;

    @Autowired
    private RemoteHotelService remoteHotelService;

    @Autowired
    private RemoteTrainService remoteTrainService;


    /**
     * 查询贵宾厅
     *
     * @param viphallId 贵宾厅ID
     * @return 贵宾厅
     */
    @Override
    public Viphall selectViphallById(Long viphallId) {
        return viphallMapper.selectViphallById(viphallId);
    }

    /**
     * 查询贵宾厅列表
     *
     * @param viphall 贵宾厅
     * @return 贵宾厅
     */
    @Override
    public List<Viphall> selectViphallList(Viphall viphall) {
        //TODO 部门
        List<Viphall> viphalls = viphallMapper.selectViphallList(viphall);
//        String deptName = remoteCustomerService.getInfo(1L).getData().getDeptName();
        return viphalls;
        //return null;
    }

    @Override
    public List<Viphall> selectViphallLists(Viphall viphall)
    {
        return viphallMapper.selectViphallLists(viphall);
    }
    /**
     * 新增贵宾厅
     *
     * @param viphall 贵宾厅
     * @return 结果
     */
    @Override
    public int insertViphall(Viphall viphall) {
        viphall.setCreateTime(DateUtils.getNowDate());
        return viphallMapper.insertViphall(viphall);
    }

    /**
     * 修改贵宾厅
     *
     * @param viphall 贵宾厅
     * @return 结果
     */
    @Override
    public int updateViphall(Viphall viphall) {
        viphall.setUpdateTime(DateUtils.getNowDate());
        return viphallMapper.updateViphall(viphall);
    }

    /**
     * 批量删除贵宾厅
     *
     * @param viphallIds 需要删除的贵宾厅ID
     * @return 结果
     */
    @Override
    public int deleteViphallByIds(Long[] viphallIds) {
        return viphallMapper.deleteViphallByIds(viphallIds);
    }

    /**
     * 删除贵宾厅信息
     *
     * @param viphallId 贵宾厅ID
     * @return 结果
     */
    @Override
    public int deleteViphallById(Long viphallId) {
        return viphallMapper.deleteViphallById(viphallId);
    }

    /**
     * 贵宾厅积分兑换价维护
     * 市场价100（市场价 * 100 - 8000）积分 + 79元 = 2000积分 + 79元
     */
    @Override
    public int conVipHallTotal(Map<String, Object> resultMap) {
        //TODO 接收前台返回数据
        Integer money1 = (Integer) resultMap.get("money1");
        Integer total = (Integer)resultMap.get("total");
        Integer money2 = (Integer)resultMap.get("money2");
        //获取实时市场价
        Integer marketPrice = 100;
        //计算出最终价格返回给前台
        Integer sumMoney = (marketPrice * money1 - total) + money2;

        return sumMoney;
    }

    /**
     * 贵宾厅合同、价格维护
     *
     * @param contractLinkViphall
     * @return
     */
    @Override
    public boolean addContractLinkViphall(ContractLinkViphall contractLinkViphall) {
        boolean flag = false;
        int count = 0;
        //先添加贵宾厅合同中间表数据
        contractLinkViphall.setCreateTime(DateUtils.getNowDate());
        int result = contractLinkViphallMapper.insertContractLinkViphall(contractLinkViphall);
        if (result > 0) {
            //获取新增后的主键值
            Long contractViphallId = contractLinkViphall.getContractViphallId();
            List<ContractLinkViphallPrice> contractLinkViphallPrice = contractLinkViphall.getContractLinkViphallPrice();
            for (ContractLinkViphallPrice linkViphallPrice : contractLinkViphallPrice) {
                //封装数据
                ContractLinkViphallPrice viphallPrice = new ContractLinkViphallPrice();
                viphallPrice.setContractViphallId(contractViphallId);
                viphallPrice.setDayid(linkViphallPrice.getDayid());
                viphallPrice.setBasePrice(linkViphallPrice.getBasePrice());
                viphallPrice.setRecommendedRetailPrice(linkViphallPrice.getRecommendedRetailPrice());
                viphallPrice.setSalePrice(linkViphallPrice.getSalePrice());
                viphallPrice.setStatus(linkViphallPrice.getStatus());
                viphallPrice.setUpdateTime(linkViphallPrice.getUpdateTime());
                viphallPrice.setCreateTime(DateUtils.getNowDate());
                //执行保存操作(贵宾厅价格数据)
                count = contractLinkViphallPriceMapper.insertContractLinkViphallPrice(viphallPrice);
            }
        }
        if (count > 0) {
            flag = true;
        }
        return flag;
    }

    /**
     * 贵宾厅价格维护
     * @param viphallPrice 价格对象
     * @return true 成功 | fasle 失败
     */
    @Override
    public boolean saveOrDelVipHallPrice(List<ContractLinkViphallPrice> viphallPrice) {
        boolean flag = false ;
        int result = this.saveOrDelPrice(viphallPrice);
        if(result > 0){
            flag = true ;
        }
        return flag;
    }

    @Override
    public Viphall selectContraceViphall(Long viphallId) {
        return viphallMapper.selectContraceViphall(viphallId);
    }

    /**
     * 获取所有贵宾厅列表
     * @return
     */
    @Override
    public List<Viphall> getAll() {
        return viphallMapper.getAll();
    }

    /**
     * 保存或者删除贵宾厅价格
     * @param viphallPrice
     * @return
     */
    private int saveOrDelPrice(List<ContractLinkViphallPrice> viphallPrice){
        //先根据服务id查询数据库中的数据
        int result = 0;
        ContractLinkViphallPrice linkViphallPrice = null;
        if(viphallPrice !=null && viphallPrice.size()>0){
            for (ContractLinkViphallPrice contractLinkViphallPrice : viphallPrice) {
                linkViphallPrice = new ContractLinkViphallPrice();
                linkViphallPrice.setContractViphallId(contractLinkViphallPrice.getContractViphallId());
                break;
            }

        }

        List<ContractLinkViphallPrice> contractLinkViphallPrices =
                contractLinkViphallPriceMapper.selectListByContractVipHallId(linkViphallPrice.getContractViphallId());

        //遍历页面传递的数据
        for (ContractLinkViphallPrice contractLinkViphallPrice : viphallPrice) {
            //遍历数据表中的数据
            for (ContractLinkViphallPrice price : contractLinkViphallPrices) {
                //如果页面中、数据表的价格id都相同，则是修改
                if(contractLinkViphallPrice.getContractViphallPriceId()!=null &&
                        contractLinkViphallPrice.getContractViphallPriceId().equals(price.getContractViphallPriceId())){
                    contractLinkViphallPrice.setUpdateTime(DateUtils.getNowDate());
                    result =  contractLinkViphallPriceMapper.updateContractLinkViphallPrice(contractLinkViphallPrice);
                    result ++ ;
                }
                //如果页面传递的数据没有价格id，则是新增
                else if(contractLinkViphallPrice.getContractViphallPriceId() == null ){
                    contractLinkViphallPrice.setCreateTime(DateUtils.getNowDate());
                    result = contractLinkViphallPriceMapper.insertContractLinkViphallPrice(contractLinkViphallPrice);
                    result ++ ;
                }
            }
        }

        //删除数据表中的数据
        for (int i = 0; i < contractLinkViphallPrices.size(); i++) {
            //数据表中的合同价格id
            long vipHallPriceId =contractLinkViphallPrices.get(i).getContractViphallPriceId();
            //页面中的合同价格id和数据表中的合同价格id比较
            if (!viphallPrice.stream().anyMatch(item->item.getContractViphallPriceId().equals(vipHallPriceId))) {
                //删除
                result = contractLinkViphallPriceMapper.deleteContractLinkViphallPriceById(vipHallPriceId);
                result ++ ;
            }
        }

        return result;
    }

    @Override
    public List<ViphallVo> queryAddress(ViphallVo viphallVo) {
        //TODO 获取用户定位信息
        return viphallMapper.queryAddress(viphallVo);
    }

    @Override
    public List<KtserviceViphallTag> selectKtserviceViphallTagByIdToTagName(Long viphallId) {
        return viphallMapper.selectKtserviceViphallTagByIdToTagName(viphallId);
    }

    /**
     * 根据贵宾厅ID查询贵宾厅设施
     * @param viphallId
     * @return
     */
    @Override
    public List<KtserviceViphallAuxtype> selectKtserviceViphalllAuxtypeByAuxtype(Long viphallId) {
        return viphallMapper.selectKtserviceViphalllAuxtypeByAuxtype(viphallId);
    }

    /**
     * 通过当前选择的机场三字码推荐贵宾厅
     * @param airportCode
     * @return
     */
    @Override
    public List<Map<String, Object>> queryAirportCode(String airportCode) {
        List<Viphall> viphalls = viphallMapper.queryAirportCode(airportCode);
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Viphall viphall : viphalls) {
            Map<String, Object> map = new HashMap<>();
            map.put("viphallId", viphall.getViphallId());
            map.put("viphallName", viphall.getViphallName());
            String businessBeginhour = viphall.getBusinessBeginhour().toString();
            if (businessBeginhour != null) {
                StringBuilder builder = new StringBuilder(businessBeginhour);
                builder.insert(builder.length() - 2, ":");
                map.put("businessBeginhour", builder.toString());
            }
            String businessEndhour = viphall.getBusinessEndhour().toString();
            if (businessEndhour != null) {
                StringBuilder stringBuilder = new StringBuilder(businessEndhour);
                stringBuilder.insert(stringBuilder.length() - 2, ":");
                map.put("businessEndhour", stringBuilder.toString());
            }
            map.put("securityCheckLocation", viphall.getSecurityCheckLocation());
            map.put("viphallCategory", viphall.getViphallCategory());
            map.put("score", "4.5");
            map.put("viphallSetOutType", viphall.getViphallSetOutType());
            if (viphall.getAirportTerminalId() != null && !viphall.getAirportTerminalId().equals(0L)) {
                Map<String, Object> objectMap = new HashMap<>();
                objectMap.put("terminalId", viphall.getAirportTerminalId());
                R<Terminal> terminalR = remoteFlightService.flightTerminalGetInfo(objectMap);
                if (terminalR.getCode() == 200 && terminalR.getData() != null){
                    map.put("startTerminal", terminalR.getData().getTerminalName());
                }
            }
            mapList.add(map);
        }
        return mapList;
    }

    /**
     * 通过当前选择的机场三字码推荐贵宾厅服务列表
     * @param airportCode
     * @return
     */
    @Override
    public List<ViphallList> queryAirportCodeList(String airportCode) {
        List<ViphallList> viphallLists = viphallMapper.queryAirportCodeList(airportCode);
        StringBuilder builder;
        for (ViphallList viphallList : viphallLists) {
            String businessBeginhour = viphallList.getBusinessBeginhour();
            if (businessBeginhour != null) {
                builder = new StringBuilder(businessBeginhour);
                builder.insert(builder.length() - 2, ":");
                viphallList.setBusinessBeginhour(String.valueOf(builder));
            }
            String businessEndhour = viphallList.getBusinessEndhour();
            if (businessEndhour != null) {
                builder = new StringBuilder(businessEndhour);
                builder.insert(builder.length() - 2, ":");
                viphallList.setBusinessEndhour(String.valueOf(builder));
            }
            if (viphallList.getAirportTerminalId() != null && !viphallList.getAirportTerminalId().equals(0L)) {
                Map<String, Object> objectMap = new HashMap<>();
                objectMap.put("terminalId", viphallList.getAirportTerminalId());
                R<Terminal> terminalR = remoteFlightService.flightTerminalGetInfo(objectMap);
                if (terminalR.getCode() == 200 && terminalR.getData() != null){
                    viphallList.setStartTerminal(terminalR.getData().getTerminalName());
                }
            }
            String score = "4.4";
            viphallList.setScore(score);
        }
        return viphallLists;
    }

    /**
     * 贵宾厅服务详情页
     * @param viphallId
     * @return
     */
    @Override
    public Map<String, Object> queryViphallId(Long viphallId) {
        Viphall viphall = viphallMapper.queryViphallId(viphallId);
        Map<String, Object> map = new HashMap<>();
        map.put("viphallId", viphall.getViphallId());
        map.put("viphallName", viphall.getViphallName());
        map.put("viphallCategory", viphall.getViphallCategory());
        String businessBeginhour = viphall.getBusinessBeginhour();
        if (businessBeginhour != null) {
            StringBuilder builder = new StringBuilder(businessBeginhour);
            builder.insert(builder.length() - 2, ":");
            map.put("businessBeginhour", builder.toString());
        }
        String businessEndhour = viphall.getBusinessEndhour();
        if (businessEndhour != null) {
            StringBuilder stringBuilder = new StringBuilder(businessEndhour);
            stringBuilder.insert(stringBuilder.length() - 2, ":");
            map.put("businessEndhour", stringBuilder);
        }
        map.put("freeMember", viphall.getFreeMember());
        String locationPoint = viphall.getLocationPoint();
        if (locationPoint != null) {
            map.put("longitude", locationPoint.split(",")[0]);
            map.put("latitude", locationPoint.split(",")[1]);
        }
        map.put("address", viphall.getAddress());
        map.put("share", viphall.getShare());
        map.put("overallScore", viphall.getAppraise());
        map.put("basePrice", viphall.getBasePrice());
        map.put("recommendedRetailPrice", viphall.getPrice());
        map.put("salePrice", viphall.getSellPrice());
        map.put("accompanyingPrice", "");

        List<Map<String, Object>> ktserviceViphallTagList = new ArrayList<>();
        for (KtserviceViphallTag ktserviceViphallTag : viphall.getKtserviceViphallTagList()) {
            Map<String, Object> tagMap = new HashMap<>();
            tagMap.put("tagName", ktserviceViphallTag.getTagName());
            tagMap.put("tagColor", ktserviceViphallTag.getTagColor());
            ktserviceViphallTagList.add(tagMap);
        }
        map.put("ktserviceViphallTag", ktserviceViphallTagList);
        List<Map<String, Object>> ktserviceViphallAuxtypeList = new ArrayList<>();
        for (KtserviceViphallAuxtype ktserviceViphallAuxtype : viphall.getKtserviceViphallAuxtypeList()) {
            Map<String, Object> auxtMap = new HashMap<>();
            auxtMap.put("auxtypeName", ktserviceViphallAuxtype.getAuxtypeName());
            auxtMap.put("iconPath", ktserviceViphallAuxtype.getIconPath());
            ktserviceViphallAuxtypeList.add(auxtMap);
        }
        map.put("ktserviceViphallAuxtype", ktserviceViphallAuxtypeList);
        map.put("viphallImagedesc", viphall.getViphallImagedescList().get(0).getImageurl());
        List<Map<String, Object>> viphallServiceInstructionsList = new ArrayList<>();
        for (ViphallServiceInstructions viphallServiceInstructions : viphall.getViphallServiceInstructionsList()) {
            Map<String, Object> instructionsMap = new HashMap<>();
            instructionsMap.put("packageName", viphallServiceInstructions.getPackageName());
            instructionsMap.put("imgurl", viphallServiceInstructions.getImgurl());
            String content = viphallServiceInstructions.getContent();
            if (content != null) {
                List packageContentList = new ArrayList();
                packageContentList.add(content.split(",")[0]);
                packageContentList.add(content.split(",")[1]);
                packageContentList.add(content.split(",")[2]);
                instructionsMap.put("packageContent", packageContentList);
            }
            viphallServiceInstructionsList.add(instructionsMap);
        }
        map.put("viphallServiceInstructions", viphallServiceInstructionsList);
        List<Map<String, Object>> viphallCommentList = new ArrayList<>();
        Long id = viphall.getViphallId();
        List<ViphallComment> viphallComments = viphallCommentService.queryComment(id);
        for (ViphallComment viphallComment : viphallComments) {
            Map<String, Object> commentMap = new HashMap<>();
            commentMap.put("commentContent", viphallComment.getCommentContent());
            commentMap.put("score", viphallComment.getCommentScore());
            viphallCommentList.add(commentMap);
        }
        map.put("viphallComment", viphallCommentList);
        List attachPathList = new ArrayList();
        for (ViphallAttachment viphallAttachment : viphall.getViphallAttachmentList()) {
            String attachPath = viphallAttachment.getAttachPath();
            attachPathList.add(attachPath);
        }
        map.put("viphallAttachment", attachPathList);
        return map;
    }

    @Override
    public Viphall getViphallById(Long viphallId)
    {
        return viphallMapper.queryViphallId(viphallId);
    }

    /**
     * 通过当前贵宾厅id获取贵宾厅联系电话
     * @param viphallId
     * @return
     */
    @Override
    public String selectVipHallPhoneById(Long viphallId) {
        return viphallMapper.selectVipHallPhoneById(viphallId);
    }

    /**
     * 根据机场三字码查询
     * */
    @Override
    public boolean selectVipHallVoByAirportCode(String airportCode) {
        int i = viphallMapper.selectVipHallVoByAirportCode(airportCode);
        boolean flag = false;
        if (i > 0){
            flag = true;
        }
        return flag;
    }

    /**
     * 常用服务
     * @param customerId
     * @return
     */
    @Override
    public List<ViphallCommonServices> commonServices(Long customerId) {
        List<ViphallCommonServices> list = new ArrayList<>();
        List<ViphallCommon> viphallCommons = viphallMapper.commonServices(customerId);
        StringBuilder builder;
        for (ViphallCommon viphallCommon : viphallCommons) {
            ViphallCommonServices viphallCommonServices = new ViphallCommonServices();
            viphallCommonServices.setID(viphallCommon.getViphallId());
            viphallCommonServices.setName(viphallCommon.getViphallName());
            viphallCommonServices.setAddress(viphallCommon.getAddress());
            String businessBeginhour = viphallCommon.getBusinessBeginhour();
            if (businessBeginhour != null) {
                builder = new StringBuilder(businessBeginhour);
                builder.insert(builder.length() - 2, ":");
                viphallCommonServices.setBusinessBeginhour(builder.toString());
            }
            String businessEndhour = viphallCommon.getBusinessEndhour();
            if (businessEndhour != null) {
                builder = new StringBuilder(businessEndhour);
                builder.insert(builder.length() - 2, ":");
                viphallCommonServices.setBusinessEndhour(builder.toString());
            }
            viphallCommonServices.setSecurityCheckLocation(viphallCommon.getSecurityCheckLocation());
            viphallCommonServices.setViphallCategory(viphallCommon.getViphallCategory());
            String viphallType = "viphall";
            viphallCommonServices.setType(viphallType);

            if (viphallCommon.getAirportTerminalId() != null && !viphallCommon.getAirportTerminalId().equals(0L)) {
                Map<String, Object> objectMap = new HashMap<>();
                objectMap.put("terminalId", viphallCommon.getAirportTerminalId());
                R<Terminal> terminalR = remoteFlightService.flightTerminalGetInfo(objectMap);
                if (terminalR.getCode() == 200 && terminalR.getData() != null){
                    viphallCommon.setStartTerminal(terminalR.getData().getTerminalName());
                }
            }

            String score = "4.1";
            viphallCommonServices.setScore(score);
            list.add(viphallCommonServices);
        }
        List<VipchannelCommon> vipchannelCommons = vipchannelService.commonServices(customerId);
        for (VipchannelCommon vipchannelCommon : vipchannelCommons) {
            ViphallCommonServices viphallCommonServices = new ViphallCommonServices();
            viphallCommonServices.setID(vipchannelCommon.getVipchannelId());
            viphallCommonServices.setName(vipchannelCommon.getVipchannelName());
            viphallCommonServices.setAddress(vipchannelCommon.getAddress());
            viphallCommonServices.setBusinessBeginhour(vipchannelCommon.getBusinessBeginHour());
            viphallCommonServices.setBusinessEndhour(vipchannelCommon.getBusinessEndHour());
            String vipchannelType = "vipchannel";
            viphallCommonServices.setType(vipchannelType);
            viphallCommonServices.setStartTerminal(vipchannelCommon.getStartTerminal());
            viphallCommonServices.setScore(vipchannelCommon.getScore());
            list.add(viphallCommonServices);
        }
        return list;
    }

    @Override
    public VipHallVipChannelPrice selectVipHallPrice(Long id) {
        VipHallVipChannelPrice price = new VipHallVipChannelPrice();
        ContractLinkViphallPrice viphallPrice = viphallMapper.selectVipHallPrice(id);
        price.setBasePrice(viphallPrice.getBasePrice());
        price.setRecommendedRetailPrice(viphallPrice.getRecommendedRetailPrice());
        price.setSalePrice(viphallPrice.getSalePrice());
        return price;
    }

    /**
     * 添加贵宾厅
     * @param viphall
     * @return
     */
    @Override
    public int saveVipHall(Viphall viphall) throws Exception {
//        Viphall viphall = new Viphall();
//        viphall.setCityId(viphallPatamter.getCityId());//所属城市
//        viphall.setAirportTerminalId(viphallPatamter.getAirportTerminalId());//航站楼ID
//        viphall.setDeptId(viphallPatamter.getDeptId());//部门
//        viphall.setViphallLocationTypeid(viphallPatamter.getViphallLocationTypeid());//所属分类
//        viphall.setViphallName(viphallPatamter.getViphallName());//贵宾厅名称
//        viphall.setAddress(viphallPatamter.getAddress());//贵宾厅地址
//        viphall.setCustomerServiceTel(viphallPatamter.getCustomerServiceTel());//贵宾厅电话
//        viphall.setLocationPoint(viphallPatamter.getLocationPoint());//经纬度
//        viphall.setOwnerEmployeeName(viphallPatamter.getOwnerEmployeeName());//店面负责人
//        viphall.setOwnerEmployeePhone(viphallPatamter.getOwnerEmployeePhone());//负责人电话
//        viphall.setSecurityCheckLocation(viphallPatamter.getSecurityCheckLocation());//安检位置 1 安检内 2 安检外 3要客(出个枚举)
//        viphall.setViphallStatus(viphallPatamter.getViphallStatus());//状态 0， 不可用 1， 可用
//        viphall.setViphallSetOutType(viphallPatamter.getViphallSetOutType());//出发区域 0国内  1 国际
//        viphall.setIsDeleted("false");
//        //所属机场(通过机场名称来获取机场三子码)
//        viphall.setAirportCode(viphallPatamter.getAirportCode());
//        //时间
//        String[] vipStart = viphallPatamter.getBusinessBeginhour().split(":");
//        String ss = new String();
//        for (int i = 0; i < vipStart.length; i++)
//        {
//            ss = ss + vipStart[i];
//        }
//
//        viphall.setBusinessBeginhour(ss);
//        String[] vipEnd = viphallPatamter.getBusinessEndhour().split(":");
//        String ee = new String();
//        for (int i = 0; i < vipEnd.length; i++)
//        {
//            ee = ee + vipEnd[i];
//        }
//        viphall.setBusinessEndhour(ee);
//        //vip服务类型
//        viphall.setViphallCategory(viphallPatamter.getViphallCategory());
//        //是否包含VIP通道
//        viphall.setIsIncludeVipchannel(viphallPatamter.getIsIncludeVipchannel());
//        if (viphall.getIsIncludeVipchannel().equals(EmHelper.isIncludeVipchannel.yes.getValue()))
//        {
//            viphall.setVipchannelAddress(viphallPatamter.getVipchannelAddress());
//        } else
//        {
//            viphall.setVipchannelAddress("");
//        }
//        //底价
//        viphall.setBasePrice(viphallPatamter.getBasePrice());
//        //原价
//        viphall.setPrice(viphallPatamter.getPrice());
//        //销售价不为空时
//        if (viphallPatamter.getSellPrice() != null)
//        {
//            //如何判断走的的是会员还是积分(前端判断)
//            viphall.setSellPrice(viphallPatamter.getSellPrice());
//        } else
//        {
//            viphall.setSellPrice(viphallPatamter.getPrice());
//        }
        //获取当前时间
        Date date = new Date();
//        int rows = 0;

        //城市名称转id
        if (viphall.getCityName() != null && !viphall.getCityName().equals("")){
            CityVoApi cityVoApi = new CityVoApi();
            cityVoApi.setCityCode(viphall.getCityName());
            cityVoApi = remoteTrainService.cityVo(cityVoApi);
            viphall.setCityId(Long.valueOf(cityVoApi.getCityId()));
        }

        //机场/火车站  名称转三字码
        if (viphall.getAirportName() != null && !viphall.getAirportName().equals("")){
            //viphall.getAirportCodeType() == 0:机场?火车站
            //机场
            if (viphall.getAirportCodeType() == 0){
                Map<String, Object> map = new HashMap<>();
                map.put("airportId", viphall.getAirportName());
                AirportApi airportInfoByAirportName = remoteFlightService.getAirportInfoByAirportName(map);
                viphall.setAirportCode(airportInfoByAirportName.getAirportId());
            }
            //火车站
            else if (viphall.getAirportName() != null && !viphall.getAirportName().equals("")){
                Map<String, Object> map = new HashMap<>();
                map.put("gaodename", viphall.getAirportName());
                String airportCode = remoteTrainService.selectCityId(map);
                viphall.setAirportCode(airportCode);
            }
        }

        //航站楼名称转id
        if (viphall.getTerminalName() != null && !viphall.getTerminalName().equals("")){
            viphall.setAirportTerminalId(Long.valueOf(viphall.getTerminalName()));
        }

        //贵宾厅id == null 为新增
        if (viphall.getViphallId() == null || viphall.getViphallId() == 0)
        {
            viphall.setIsDeleted("false");
            viphall.setCreateTime(date);
            viphall.setUpdateTime(date);
            viphall.setVarifyEquipment("0");
            String businessBeginhour = viphall.getBusinessBeginhour();
            if (businessBeginhour.substring(0, 1).equals("0")){
                businessBeginhour = businessBeginhour.substring(1, 2) + businessBeginhour.substring(3, 5);
            }
            else {
                businessBeginhour = businessBeginhour.substring(0, 2) + businessBeginhour.substring(3, 5);
            }
            viphall.setBusinessBeginhour(businessBeginhour);

            String businessEndhour = viphall.getBusinessEndhour();
            if (businessEndhour.substring(0, 1).equals("0")){
                businessEndhour = businessEndhour.substring(1, 2) + businessEndhour.substring(3, 5);
            }
            else {
                businessEndhour = businessEndhour.substring(0, 2) + businessEndhour.substring(3, 5);
            }
            viphall.setBusinessEndhour(businessEndhour);

            int rows = viphallMapper.insertViphall(viphall);
            if (rows > 0)
            {
                //添加贵宾厅图文描述
                if (viphall.getViphallImagedescList() != null){
                    for (ViphallImagedesc viphallImagedesc : viphall.getViphallImagedescList())
                    {
                        viphallImagedesc.setViphallId(viphall.getViphallId());
                        viphallImagedesc.setOpemployeeId(viphall.getEmploeeId());
                        viphallImagedesc.setCreateTime(date);
                        viphallImagedesc.setUpdateTime(date);
                        viphallImagedescMapper.insertViphallImagedesc(viphallImagedesc);
                    }
                }

                //添加贵宾厅附件（厅图片）
                if (viphall.getViphallAttachmentList() != null){
                    for (ViphallAttachment viphallAttachment : viphall.getViphallAttachmentList())
                    {
                        viphallAttachment.setViphallId(viphall.getViphallId());
                        viphallAttachment.setStatus("0");
                        viphallAttachment.setIpemployeeId(viphall.getEmploeeId());
                        viphallAttachment.setCreateTime(date);
                        viphallAttachment.setUpdateTime(date);
                        viphallAttachmentMapper.insertViphallAttachment(viphallAttachment);
                    }
                }

                //添加贵宾厅套餐
                if (viphall.getViphallServiceInstructionsList() != null){
                    for (ViphallServiceInstructions viphallServiceInstructions : viphall.getViphallServiceInstructionsList())
                    {
                        viphallServiceInstructions.setViphallId(viphall.getViphallId());
                        viphallServiceInstructionsMapper.insertViphallServiceInstructions(viphallServiceInstructions);
                    }
                }
            }
            else {
                throw new Exception("添加贵宾厅信息失败");
            }
        //贵宾厅id != null 为修改
        }else
        {
            viphall.setUpdateTime(date);
            int rows = viphallMapper.updateViphall(viphall);
            if (rows > 0)
            {
                //修改贵宾厅图文描述
                if (viphall.getViphallImagedescList() != null && viphall.getViphallImagedescList().size() > 0){
                    for (ViphallImagedesc viphallImagedesc : viphall.getViphallImagedescList())
                    {
                        viphallImagedesc.setViphallId(viphall.getViphallId());
                        viphallImagedesc.setOpemployeeId(viphall.getEmploeeId());
                        viphallImagedesc.setUpdateTime(date);
                        viphallImagedescMapper.updateViphallImagedesc(viphallImagedesc);
                    }
                }

                //修改贵宾厅附件（厅图片）
                if (viphall.getViphallAttachmentList() != null){
                    for (ViphallAttachment viphallAttachment : viphall.getViphallAttachmentList())
                    {
                        viphallAttachment.setViphallId(viphall.getViphallId());
                        viphallAttachment.setStatus("0");
                        viphallAttachment.setIpemployeeId(viphall.getEmploeeId());
                        viphallAttachment.setUpdateTime(date);
                        viphallAttachmentMapper.updateViphallAttachment(viphallAttachment);
                    }
                }

                //修改贵宾厅套餐
                if (viphall.getViphallServiceInstructionsList() != null){
                    for (ViphallServiceInstructions viphallServiceInstructions : viphall.getViphallServiceInstructionsList())
                    {
                        viphallServiceInstructions.setViphallId(viphall.getViphallId());
                        viphallServiceInstructionsMapper.updateViphallServiceInstructions(viphallServiceInstructions);
                    }
                }
            }
            else {
                throw new Exception("修改贵宾厅信息失败");
            }
        }
//        int k = 0;
        //标签id集合不为空，更新贵宾厅标签
        if (viphall.getTagids() != null &&viphall.getTagids().size() > 0)
        {
            //删除原有标签
            if (viphall.getViphallId() != null && viphall.getViphallId() != 0){
                viphallLinkTagMapper.deleteViphallLinkByViphallId(viphall.getViphallId());
            }
            else {
                throw new Exception("缺少必要参数");
            }

            //添加新标签
            for (Long id : viphall.getTagids())
            {
                ViphallLinkTag viphallLinkTag = new ViphallLinkTag();
                viphallLinkTag.setTagId(id);
                viphallLinkTag.setViphallId(viphall.getViphallId());
                viphallLinkTag.setOpemployeeId(viphall.getEmploeeId());
                viphallLinkTag.setCreateTime(date);
                viphallLinkTag.setUpdateTime(date);
                int k = viphallLinkTagMapper.insertViphallLinkTag(viphallLinkTag);
                if (k <= 0){
                    throw new Exception("贵宾厅标签添加失败" + id);
                }
            }
        }

        //服务设施
        if (viphall.getAuxTypeIdList() != null && viphall.getAuxTypeIdList().size() > 0){
            //删除原有贵宾厅服务
            if (viphall.getViphallId() != null && viphall.getViphallId() != 0){
                viphalllLinkAuxtypeMapper.deleteViphalllLinkAuxtypeById(viphall.getViphallId());
            }
            else {
                throw new Exception("缺少必要参数");
            }
            //添加新贵宾厅服务
            for (ViphalllLinkAuxtype viphalllLinkAuxtype : viphall.getAuxTypeIdList()) {
                viphalllLinkAuxtype.setViphallId(viphall.getViphallId());
                viphalllLinkAuxtype.setOpemployeeId(viphall.getEmploeeId());
                viphalllLinkAuxtype.setCreateTime(date);
                viphalllLinkAuxtype.setUpdateTime(date);
                viphalllLinkAuxtype.setOpemployeeId(viphall.getEmploeeId());
                int i = viphalllLinkAuxtypeMapper.insertViphalllLinkAuxtype(viphalllLinkAuxtype);
                if (i <= 0){
                    throw new Exception("贵宾厅服务添加失败" + viphalllLinkAuxtype.getAuxtypeId());
                }
            }
        }

        return 1;

//        if (k > 0 && rows > 0)
//        {
//            return rows;
//        } else
//        {
//            return 0;
//        }
    }

    /**
     * 贵宾厅基础列表
     * */
    @Override
    public List<Viphall> viphallList(TmpViphallVo tmpViphallVo) {
        if (tmpViphallVo.getCity() != null && !tmpViphallVo.getCity().equals("")){
            CityVoApi cityVoApi = new CityVoApi();
            cityVoApi.setCityCode(tmpViphallVo.getCity());
            cityVoApi = remoteTrainService.cityVo(cityVoApi);
            if (cityVoApi != null){
                tmpViphallVo.setCityId(cityVoApi.getCityId());
            }
        }

        List<Viphall> viphalls = viphallMapper.viphallList(tmpViphallVo);

        if (viphalls.size() > 0){
            for (Viphall viphall : viphalls) {
                if (viphall.getBusinessBeginhour() != null){
                    String businessBeginhour = viphall.getBusinessBeginhour();
                    String time = businessBeginhour.substring(0, businessBeginhour.length() - 2) + ":" + businessBeginhour.substring(businessBeginhour.length() - 2);
                    viphall.setBusinessBeginhour(time);
                }
                if (viphall.getBusinessEndhour() != null) {
                    String businessEndhour = viphall.getBusinessEndhour();
                    String time = businessEndhour.substring(0, businessEndhour.length() - 2) + ":" + businessEndhour.substring(businessEndhour.length() - 2);
                    viphall.setBusinessEndhour(time);
                }
                CityVoApi cityVoApi = new CityVoApi();
                cityVoApi.setCityId(String.valueOf(viphall.getCityId()));
                CityVoApi cityVo = remoteTrainService.cityVo(cityVoApi);
                if (cityVo != null){
                    viphall.setCityName(cityVo.getCityName());
                    viphall.setCityCode(cityVo.getCityCode());
                }

                Map<String, Object> tmpMap = new HashMap<>();
                tmpMap.put("airportId", viphall.getAirportCode());
                AirportApi airportApi = remoteFlightService.getAirportInfoByAirportName(tmpMap);
                if (airportApi != null){
                    viphall.setAirportName(airportApi.getAirportName());
                }

                if (viphall.getAirportTerminalId() != null && !viphall.getAirportTerminalId().equals(0L)) {
                    Map<String, Object> objectMap = new HashMap<>();
                    objectMap.put("terminalId", viphall.getAirportTerminalId());
                    R<Terminal> terminalR = remoteFlightService.flightTerminalGetInfo(objectMap);
                    if (terminalR.getCode() == 200 && terminalR.getData() != null){
                        viphall.setTerminalName(terminalR.getData().getTerminalName());
                    }
                }

                ViphallServiceInstructions serviceInstructions = new ViphallServiceInstructions();
                serviceInstructions.setViphallId(viphall.getViphallId());
                List<ViphallServiceInstructions> viphallServiceInstructions = viphallServiceInstructionsMapper.selectViphallServiceInstructionsList(serviceInstructions);
                if (viphallServiceInstructions != null && viphallServiceInstructions.size() > 0)
                    viphall.setViphallServiceInstructionsList(viphallServiceInstructions);
            }
            return viphalls;
        }
        else {
            return new ArrayList<>();
        }
    }

    /**
     * 删除贵宾厅信息
     * */
    @Override
    public int delViphallVo(Long id) {
        int i = viphallMapper.delViphallVo(id);
        return i;
    }
}
